package code;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import dominain.AbstractExeDTO;
import error.JException;

public class CategoryComponetImpl implements CategoryComponet {
	/** The properties. */
	private Properties properties = null;
	/**
	 * properties 位置的class
	 */
	private AbstractExeDTO exe = null;

	/**
	 * Instantiates a new love code componet impl.
	 */
	public CategoryComponetImpl(AbstractExeDTO exe) {
		if (null == properties) {
			properties = new Properties();

		}
		properties.clear();
		this.exe = exe;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.controller.love.component.error.CategoryCodeComponet#getCodeByContent
	 * (java.lang.String, java.lang.String)
	 */
	@Override
	public String getCodeByContent(String category, String code)
			throws JException {
		try {

			properties.load(exe.getClass()
					.getResource(category + ".properties").openStream());
			return (String) properties.get(code);

		} catch (Exception e) {
			e.printStackTrace();
			throw new JException("common", e.getMessage());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.controller.love.component.error.CategoryCodeComponet#getContentByCode
	 * (java.lang.String, java.lang.String)
	 */
	@Override
	@Deprecated
	public String getContentByCode(String category, String code)
			throws JException {

		return "";

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.controller.love.component.error.CategoryCodeComponet#getRefMapping
	 * (java.lang.String)
	 */
	@Override
	public List<CategoryCode> getRefMapping(String category) throws JException {
		try {
			List<CategoryCode> loveCodes = new ArrayList<CategoryCode>();
			properties.clear();
			properties.load(exe.getClass()
					.getResource(category + ".properties").openStream());

			for (Object code : properties.keySet()) {
				CategoryCode loveCode = new CategoryCode();
				loveCode.setCode((String) code);

				loveCode.setMsg(properties.getProperty((String) code));
				loveCodes.add(loveCode);
			}
			return loveCodes;
		} catch (Exception e) {
			throw new JException("XXXX");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.controller.love.component.error.CategoryCodeComponet#
	 * getContentFromErrorTable(java.lang.String)
	 */
	@Override
	public String getContentFromErrorTable(String code) throws JException {
		try {

			properties.load(exe.getClass().getResource("msg.properties")
					.openStream());
			return (String) properties.get(code);

		} catch (Exception e) {
			throw new JException("XXXX");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.controller.love.component.error.CategoryCodeComponet#getRefMappingContent
	 * (java.lang.String)
	 */
	@Override
	public String[] getRefMappingContent(String category) throws JException {
		List<CategoryCode> codes = this.getRefMapping(category);

		String[] contents = new String[codes.size()];
		for (int i = 0; i < codes.size(); i++) {
			contents[i] = codes.get(i).getMsg();
		}
		return contents;
	}

}
